import { useEffect, useState, useRef } from 'react'

function useCallbackState(state: any) {
  const cbRef = useRef({})
  const [data, setData] = useState(state)

  useEffect(() => {
    // cbRef.current && cbRef.current(data);
    cbRef.current = data
  }, [data])

  return [
    data,
    (val: any, callback: () => void) => {
      cbRef.current = callback
      setData(val)
    },
  ]
}

export default useCallbackState
